import { getThemeFontSize } from "@/utils";

/**
 * 页面加载设置主题方法
 * */
export const switchTheme = (colorInfo: Record<string, any>,) => {
  try {
    const themeID = '#themeID';
    let themeInnerHtml = '';

    const primaryColor = colorInfo?.['@primary-color']; //主色
    const successColor = colorInfo?.['@success-color']; //成功色
    const warningColor = colorInfo?.['@warning-color']; //警告色
    const errorColor = colorInfo?.['@error-color']; //失败色
    const headBackground = colorInfo?.['@layout-header-background']; //系统头部
    const fontSizeBase: number = getThemeFontSize() ? Number(getThemeFontSize()) : 14  // 系统字号


    /** 字体设置 */
    if (fontSizeBase) {
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-card .ant-card-head{font-size:${fontSizeBase + 2}px}`
      themeInnerHtml += `${themeID} .search-box-switch{font-size:${fontSizeBase}px} .theme-search-input{font-size:${fontSizeBase}px !important;padding-top:${18 - fontSizeBase}px !important;padding-bottom:${18 - fontSizeBase}px !important}`
      themeInnerHtml += `${themeID} .search-box-btn>.ant-btn{font-size:${fontSizeBase}px} .search-box-btn>.ant-btn>.anticon{font-size:${fontSizeBase - 2}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-table.ant-table-small{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-btn{font-size:${fontSizeBase}px;padding-top:${18 - fontSizeBase}px;padding-bottom:${18 - fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-input-affix-wrapper{font-size:${fontSizeBase}px;padding-top:${18 - fontSizeBase}px;padding-bottom:${18 - fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-form-item-label>label{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-select{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-input-number{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-radio{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-form label{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-empty{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-pagination{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .anticon{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-alert{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-alert-message{font-size:${fontSizeBase + 2}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .ant-alert-description{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-pane-layout .theme-pane-color{font-size:${fontSizeBase}px}`
      themeInnerHtml += `${themeID} .theme-edit-side .theme-edit-side-logo{font-size:${fontSizeBase + 4}px !important} `;
      themeInnerHtml += `${themeID} .theme-edit-side .ant-menu,.ant-menu-item-icon{font-size:${fontSizeBase}px !important}`;
      themeInnerHtml += `${themeID} .theme-edit-side .ant-menu,.ant-tabs{font-size:${fontSizeBase}px !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .ant-tabs-tab-btn{font-size:${fontSizeBase}px}.ant-tabs-tab-remove>.anticon{font-size:${fontSizeBase - 2}px}`;
    }

    /** 头部主题色 */
    if (headBackground) {
      themeInnerHtml += `${themeID} .theme-edit-side .theme-edit-side-logo{background: ${headBackground}}`
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-head{background: ${headBackground}}`
    }

    /*  左侧菜单 以及主题色按钮修改 */
    if (primaryColor) {
      themeInnerHtml += `${themeID} .theme-edit-side{background:${primaryColor} !important;}`;
      themeInnerHtml += `${themeID} .theme-edit-side .ant-menu-item-selected{background:${primaryColor} !important}`;
      themeInnerHtml += `${themeID} .theme-edit-side .theme-submenu-title{color:${primaryColor}}`;
      themeInnerHtml += `${themeID} .theme-edit-side .theme-submenu-title .ant-menu-submenu-title:hover{color:${primaryColor}}`;
      themeInnerHtml += `${themeID} .theme-edit-side .theme-submenu-title .theme-menu-item:hover{color:${primaryColor} !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs  .ant-tabs-tab.ant-tabs-tab-active .ant-tabs-tab-btn{color:${primaryColor};}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .ant-tabs-tab:hover{color:${primaryColor}}`;

      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .theme-pane-alert .ant-alert-info{border-color:${primaryColor}}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .theme-pane-alert .ant-alert-info .ant-alert-icon{color:${primaryColor}}`;

      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-default{color:${primaryColor};border-color:${primaryColor}}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-default:hover, .ant-btn-default:focus, .ant-btn-default:active{background:${primaryColor} !important;color:#fff !important;}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-primary{background:${primaryColor};border-color:${primaryColor};color:#fff}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-link{color: ${primaryColor};}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-link:hover, .ant-btn-link:focus, .ant-btn-link:active{background:${primaryColor} !important;color:#fff !important;border-color:transparent;}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-dashed:hover, .ant-btn-dashed:focus{color:${primaryColor} !important;border-color:${primaryColor} !important;}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-ghost:hover, .ant-btn-ghost:focus{color:${primaryColor} !important;border-color:${primaryColor} !important;}`;

      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-input-number:hover{border-color:${primaryColor} !important;}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-switch-checked{background-color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-radio-checked .ant-radio-inner{border-color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-radio-inner::after{background-color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled)::before{background-color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-checkbox-checked .ant-checkbox-inner{background-color:${primaryColor}  !important;border-color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-checkbox-indeterminate .ant-checkbox-inner::after{background-color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-checkbox-disabled .ant-checkbox-inner{background-color: #f5f5f5 !important;border-color: #d9d9d9 !important;}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-radio-button-wrapper-checked{border-color:${primaryColor}  !important;color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-radio-button-wrapper:hover{color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-input:hover{border-color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-input:focus, .ant-input-focused{border-color:${primaryColor}  !important; box-shadow:none}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-input-number:focus, .ant-input-number-focused{border-color:${primaryColor}  !important; box-shadow:none}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-select:not(.ant-select-disabled):hover .ant-select-selector{border-color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-select-focused:not(.ant-select-disabled).ant-select:not(.ant-select-customize-input) .ant-select-selector{border-color:${primaryColor}  !important; box-shadow:none}`;

      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-pagination-item-active{border-color:${primaryColor};}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-pagination-item-active a{color:${primaryColor};}`;

      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .search-box .search-box-switch .switch-font-target{color:${primaryColor} !IMPORTANT;}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .search-box .search-box-switch .switch-font:hover{color:${primaryColor} !IMPORTANT;}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .theme-layout-icon{color:${primaryColor};}`;

      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .search-box .search-box-form-layout .search-box-form .theme-search-input .ant-input-affix-wrapper:not(.ant-input-affix-wrapper-disabled):hover{border-color:${primaryColor}  !important}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .search-box .search-box-form-layout .search-box-form .theme-search-input:focus{border-color:${primaryColor}  !important; box-shadow:none !important}`;
    }

    /* 成功色 */
    if (successColor) {
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .theme-pane-alert .ant-alert-success{border-color:${successColor}}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .theme-pane-alert .ant-alert-success .ant-alert-icon{color:${successColor}}`;
    }

    /* 警告色 */
    if (warningColor) {
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .theme-pane-alert .ant-alert-warning{border-color:${warningColor}}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .theme-pane-alert .ant-alert-warning .ant-alert-icon{color:${warningColor}}`;
    }

    /* 失败色 */
    if (errorColor) {
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .theme-pane-alert .ant-alert-error{border-color:${errorColor}}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .theme-pane-alert .ant-alert-error .ant-alert-icon{color:${errorColor}}`;

      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-dangerous{color:${errorColor};border-color:${errorColor}}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-dangerous:hover, .ant-btn-dangerous:focus{color:${errorColor} !important;border-color:${errorColor} !important; background: #fff !important}`;

      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-dangerous.ant-btn-primary{background:${errorColor}; color:#fff}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-dangerous.ant-btn-primary:hover, .ant-btn-dangerous.ant-btn-primary:focus{background:${errorColor} !important; color:#fff !important;}`;

      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .theme-pane-layout .ant-btn-dangerous.ant-btn-link{color:${errorColor};border-color: transparent !important;}`;
      themeInnerHtml += `${themeID} .theme-edit-content .theme-edit-tabs .theme-submenu-tabPane .ant-btn-dangerous.ant-btn-link:hover, .ant-btn-dangerous.ant-btn-link:focus{color:${errorColor};border-color: transparent !important;}`;
    }

    addCssByStyle(themeInnerHtml);
  } catch (e: any) {
    throw new Error(e);
  }
};

/* 添加style标签设置全局样式 */
const addCssByStyle = (themeStyleInfo: string) => {
  let themeStyle = document.getElementById('themeStyle');
  if (themeStyle) {
    themeStyle.remove();
  }

  if (themeStyleInfo) {
    let themeStyle = document.createElement('style');
    themeStyle.setAttribute('type', 'text/css');
    themeStyle.setAttribute('id', 'themeStyle');
    themeStyle.innerHTML = themeStyleInfo;
    let head: any = document.getElementsByTagName('head').item(0);
    head.appendChild(themeStyle);
  }
};
